package com.zl.cms.service;

import com.zl.basic.CommonUtil.Check;
import com.zl.basic.model.Pager;
import com.zl.cms.dao.IChannelDao;
import com.zl.cms.dao.IGroupDao;
import com.zl.cms.dao.IUserDao;
import com.zl.cms.model.*;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;
import java.util.Set;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-1-24 Time: 下午1:54
 */
@Service("groupService")
public class GroupService implements IGroupService {

  private IGroupDao groupDao;
  private IUserDao userDao;
  private IChannelDao channelDao;

  public IChannelDao getChannelDao() {
    return channelDao;
  }

  @Inject
  public void setChannelDao(IChannelDao channelDao) {
    this.channelDao = channelDao;
  }

  public IGroupDao getGroupDao() {
    return groupDao;
  }

  @Inject
  public void setGroupDao(IGroupDao groupDao) {
    this.groupDao = groupDao;
  }

  public IUserDao getUserDao() {
    return userDao;
  }

  @Inject
  public void setUserDao(IUserDao userDao) {
    this.userDao = userDao;
  }

  @Override
  public void add(Group group) {
    groupDao.add(group);
  }

  @Override
  public void delete(int id) {
    List<User> users = userDao.listUsersByGroupId(id);
    Check.checkIsNull(users, "the group has users,you can not delete the group");
    groupDao.delete(id);
  }

  @Override
  public Group load(int id) {
    return groupDao.load(id);
  }

  @Override
  public void update(Group group) {
    groupDao.update(group);
  }

  @Override
  public List<Group> listGroup() {
    return groupDao.listGroup();
  }

  @Override
  public Pager<Group> findGroup() {
    return groupDao.findGroup();
  }

  /**
   * 删除组里的用户
   * 
   * @param gid 组id
   */
  @Override
  public void deleteGroupsUsers(int gid) {
    groupDao.deleteGroupUsers(gid);
  }

  @Override
  public void cleanGroupChannel(int gid) {
	groupDao.cleanGroupChannel(gid);
  }

  @Override
  public void deleteGroupChannel(int gid, int cid) {
	groupDao.deleteGroupChannel(gid,cid);
  }

  @Override
  public void addGroupChannel(int gid, int cid) {
    Group g = groupDao.load(gid);
    Channel c = channelDao.load(cid);
    Check.checkAnyNull(g, "the group added not exist");
    Check.checkAnyNull(c, "the channel added not exist");
    groupDao.addGroupChannel(g, c);
  }

  @Override
  public GroupChannel loadGroupChannel(int gid, int cid) {
    return groupDao.loadGroupChannel(gid,cid);
  }

  @Override
  public List listGroupChannelIds(int gid) {
    return groupDao.listGroupChannelIds(gid);
  }

  @Override
  public Set<ChannelTree> generateGroupChannelTree(int gid) {
    return groupDao.generateGroupChannelTree(gid);
  }

  @Override
  public Set<ChannelTree> generateUserChannelTree(int uid) {
    return groupDao.generateUserChannelTree(uid);
  }
}
